<?php

namespace App\Http\Controllers\Admin\File;

use App\Http\Models\Admin\File\File;
use App\Http\Responses\ResponseDto;
use App\Services\Admin\FileManageService;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\View;

class FilemanageController extends Controller
{


    public function __construct(FileManageService $service)
    {
        $this->FileService = $service;
    }


    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {

        $page = null!==$request->get("page")? $request->get("page"):1;
        $type = $request->get("type");
        $querys = array();
        if($type){
            $querys = array(
                array(
                    "name"=>"extension",
                    "p_name"=>"type",
                    "value"=>$type,
                    "type"=>"="
                )
            );
        }


        $files = $this->FileService->page($page,50,$querys) ;
        $extensions = $this->FileService->fileExtensions() ;
        
        return View::make('admin.file.filemanage')
            ->withFiles($files)
            ->withExtensions($extensions)
            ->withAssets($request['ASSETS']);
    }


    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $file = File::find($id);
        @unlink ($file->path);
        if($file){
            $file->delete();
        }
        return ResponseDto::createResObj(trans("admin/message.update_success"),ResponseDto::$SUCCESS);
    }
}
